package com.example.myshop.domain;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.myshop.entity.Order;
import com.example.myshop.vo.OrderVO;
import com.example.myshop.vo.PayVO;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

public interface OrderDao extends BaseMapper<Order> {
    /**
     * 根据订单id查询详情的列表
     * @param oid
     * @return
     */
    @Select(" SELECT  uid,t_order.oid, pid,t_order.order_time,t_order.recv_name,t_order.status,\n" +
            "        t_order_item.image,t_order_item.title,t_order_item.price, t_order_item.num\n" +
            "        FROM t_order\n" +
            "        LEFT JOIN t_order_item ON t_order.oid = t_order_item.oid\n" +
            "        WHERE t_order.oid=#{oid}\n" +
            "        ORDER BY\n" +
            "        t_order_item.id DESC")
    List<OrderVO> findVOByOid(Long oid);


    /**
     * 根据用户id查询详情的列表
     * @param uid
     * @return
     */
    @Select(" SELECT  uid,t_order.oid, pid,t_order.order_time,t_order.recv_name,t_order.status,t_order_item.p_status,\n" +
            "        t_order_item.image,t_order_item.title,t_order_item.price, t_order_item.num\n" +
            "        FROM t_order\n" +
            "        LEFT JOIN t_order_item ON t_order.oid = t_order_item.oid\n" +
            "        WHERE t_order.uid=#{uid}\n" +
            "        ORDER BY\n" +
            "        t_order_item.id DESC")
    List<OrderVO> findVOByUid(Long uid);

    @Select("SELECT  uid,t_order.oid, t_order_item.pid,t_product.business_id,\n" +
            "        t_order.order_time,t_order.pay_time,t_order.recv_name,t_order.status,\n" +
            "        t_order_item.p_status,t_order_item.image,t_order_item.title,t_order_item.price, t_order_item.num\n" +
            "        FROM t_order\n" +
            "        LEFT JOIN t_order_item ON t_order.oid = t_order_item.oid\n" +
            "        LEFT JOIN t_product ON t_product.id=t_order_item.pid\n" +
            "        WHERE t_order.uid=#{uid}\n" +
            "        ORDER BY\n" +
            "        t_order_item.id DESC")
    List<OrderVO> findBVOByUid(Long uid);


    /**
     *
     *根据用户id 订单id查询详情的列表
     *
     * @param oid 订单id
     * @param uid 用户id
     * @return
     */
    @Select("SELECT  uid,t_order.oid, pid,t_order.order_time,t_order.recv_name,t_order.status,t_order_item.p_status,\n" +
            "        t_order_item.image,t_order_item.title,t_order_item.price, t_order_item.num\n" +
            "        FROM t_order\n" +
            "        LEFT JOIN t_order_item ON t_order.oid = t_order_item.oid\n" +
            "        WHERE t_order.uid=#{uid} AND t_order.oid=#{oid}\n" +
            "        ORDER BY\n" +
            "        t_order_item.id DESC")
    List<OrderVO> findVOByOUid(Long oid,Long uid);

    /**
     * 根据订单id 用户id 商品id查询用户下单的某个商品
     * @param oid
     * @param pid
     * @return
     */
    @Select(" SELECT  uid,t_order.oid, pid,t_order.order_time,t_order.recv_name,t_order.status,\n" +
            "        t_order_item.image,t_order_item.title,t_order_item.price, t_order_item.num,t_order_item.p_status\n" +
            "        FROM t_order\n" +
            "        LEFT JOIN t_order_item ON t_order.oid = t_order_item.oid\n" +
            "        WHERE t_order.oid=#{oid} AND t_order_item.pid=#{pid}\n" +
            "        ORDER BY\n" +
            "        t_order_item.id DESC")
    OrderVO findVOByOUPid(Long oid,Long pid);


    @Select(" SELECT  t_order.uid,t_order.oid, t_order_item.pid,t_order.pay_time,t_order.status,\n" +
            "        t_order_item.p_status,t_product.business_id\n" +
            "        FROM t_order\n" +
            "        LEFT JOIN t_order_item ON t_order.oid = t_order_item.oid\n" +
            "\t\t\t\tLEFT JOIN t_product ON t_product.id=t_order_item.pid\n" +
            "        WHERE t_order_item.pid=#{pid}\n" +
            "        ORDER BY\n" +
            "        t_order.oid ASC")
    List<PayVO> paySearch(Long pid);

    @Select(" SELECT   t_order.uid,t_order.pay_time,t_order.status,\n" +
            "        t_order_item.oid,t_product.business_id,t_order_item.pid,\n" +
            "        t_product.title,t_order_item.num, t_order_item.p_status,t_product.priority,t_product.price\n" +
            "        FROM t_order_item\n" +
            "        LEFT JOIN t_order ON t_order.oid = t_order_item.oid\n" +
            "\t\t\t\tLEFT JOIN t_product ON t_product.id=t_order_item.pid\n" +
            "\t\t\t\tWHERE t_product.business_id=#{businessId}\n" +
            "      ORDER BY t_order_item.pid ASC")
    List<PayVO> businessOrder(Long businessId);
//    List<PayVO> businessOrder(Integer uid);


    @Select(" UPDATE t_order_item\n" +
            "        LEFT JOIN t_order ON t_order.oid = t_order_item.oid\n" +
            "\t\tLEFT JOIN t_product ON t_product.id=t_order_item.pid\n" +
            "        SET\n" +
            "            p_status=#{pStatus}\n" +
            "        where t_order.oid=#{oid} AND t_order_item.pid=#{pid} AND t_order.status=1")
    Integer updatePStatus(Long oid,Long pid,Integer pStatus);

    @Select(" UPDATE t_product\n" +
            "\t\tLEFT JOIN t_order_item ON t_product.id=t_order_item.pid\n" +
            "        SET\n" +
            "            priority=priority+#{num}\n" +
            "        where t_order_item.oid=#{oid} AND t_order_item.pid=#{pid} AND t_order_item.p_status=3")
    Integer updatePriority(Long oid,Long pid,Integer num);

    @Select("UPDATE t_product\n" +
            "\t\tLEFT JOIN t_order_item ON t_product.id=t_order_item.pid\n" +
            "        SET\n" +
            "            priority=priority-#{num}\n" +
            "        where t_order_item.oid=#{oid} AND t_order_item.pid=#{pid} AND t_order_item.p_status=3")
    Integer updateSubPriority(Long oid,Long pid,Integer num);

}
